﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class ThayDoiCoSoService : IService<ThayDoiCoSo>
    {
        private readonly IEntityRepository<ThayDoiCoSo> _thayDoiCoSoRepository;
        public ThayDoiCoSoService(IEntityRepository<ThayDoiCoSo> thayDoiCoSoRepository)
        {
            _thayDoiCoSoRepository = thayDoiCoSoRepository;
        }

        public ThayDoiCoSo Create(ThayDoiCoSo t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _thayDoiCoSoRepository.InsertOnCommit(t);
                _thayDoiCoSoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public ThayDoiCoSo Update(ThayDoiCoSo t)
        {
            ThayDoiCoSo item;
            try
            {
                item = _thayDoiCoSoRepository.Get(t.Id);
                item.Ngay = t.Ngay;
                item.NoiDungThayDoi = t.NoiDungThayDoi;
                item.GhiChu = t.GhiChu;
                //item.Created = DateTime.Now;
                item.Modified = DateTime.Now;
                //item.CreatedBy = t.CreatedBy;
                item.ModifiedBy = t.ModifiedBy;
                item.PhieuDieuTraCoSoId = t.PhieuDieuTraCoSoId;
                _thayDoiCoSoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public ThayDoiCoSo Detail(Guid id)
        {
            var item = _thayDoiCoSoRepository.Get(id);
            return item;
        }

        public List<ThayDoiCoSo> ToList()
        {
            var item = _thayDoiCoSoRepository.GetAll().ToList();
            return item;
        }

        public void Delete(ThayDoiCoSo t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _thayDoiCoSoRepository.DeleteOnCommit(t);
                _thayDoiCoSoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
